// app.js
import {
    baseUrl
} from "./utils/request.js"
App({
    onLaunch() {
        // 展示本地存储能力
        const logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)
        // 登录
        wx.login({
            success: res => {
                var code = res.code
                wx.request({
                    url: baseUrl() + '/user/login',
                    data: {
                        code: code
                    },
                    method: "POST",
                    header: {
                        'content-type': 'application/json' // 默认值
                    },
                    success(res) {
                        if (res.data.code != 1) {
                            wx.showToast({
                                title: res.data.message,
                                icon: "error"
                            })
                            wx.redirectTo({
                                url: '../status/index?data=' + res.data.message,
                                success: function (res) {
                                    console.log("重新登录页面")
                                },
                                fail: function (res) {
                                    console.log(res, "fail")
                                },
                            })
                        } else {
                            wx.setStorageSync('token', res.data.data)
                            wx.switchTab({
                                url: '/index'
                            })
                        }
                        // console.log(wx.getStorageSync('token')) 
                        // console.log(res.data.data) 
                    }
                })
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
            }
        })
    },
    // globalData: {
    //     userInfo: null
    // }
})